package duk._0801;

import java.util.ArrayList;

/**
 * 
 * @author �����
 * Ư�� ������ ���� ����� ã�� ��
 * 
 *
 */
public class FindNearNumber {


	
/*	public static void main(String[] args) {
		int findNumber = 33;
		int subNumber[] = {34,32,6,5,98,75,66,41,48,52,55,57,95,96,99,1,5,4,8};
		Integer response[] = null;
		FindNearNumber fnn = new FindNearNumber();
		response =fnn.findNearNumber(subNumber, findNumber);
		
		for(int val : response) {
			System.out.println(val);
		}
	}*/
	public FindNearNumber() {
		
	}
	public FindNearNumber(int[] subNumber, int findNumber) {
		
	}
	
	
	public Integer[] findNearNumber(int[] subNumber, int findNumber) {
		Integer[] response = null;
		ArrayList<Integer> list = new ArrayList<Integer>();
		int initNumber = 0;
		int subtraction = 0;
		
		
		initNumber = ( (findNumber> subNumber[0]) ? (findNumber-subNumber[0]) : (subNumber[0] - findNumber) );
		
		for(int i = 0; i<subNumber.length; i++) {   
			subtraction =  (( findNumber> subNumber[i] ) ? (findNumber-subNumber[i]) : (subNumber[i] - findNumber) );

			if(subtraction<=initNumber) {
				
				if(initNumber<subtraction) {
					list.clear();
					list.add(subNumber[i]);
				}else if(initNumber==subtraction) {
					list.add(subNumber[i]);
				}
				
				initNumber=subtraction;

			}			
		}
		response = new Integer[list.size()];
		 list.toArray(response);
		 return response;
	}
	
	
}
